package JUnitTest;

import static org.junit.Assert.*;
import java.util.LinkedList;
import java.util.List;
import java.io.BufferedReader;
import java.io.FileReader;
import org.junit.Test;
import Transformation.OperationChange;
import Transformation.Parameter;
import Transformation.ParameterChange;
import Transformation.SOAPTransform;
import Transformation.WsdlTransformation;
import Transformation.XSLManager;

public class XSLManagerTest {

	@Test
	public void operationRenameParameterRename() {
		WsdlTransformation wsdlTransformation = new WsdlTransformation();
        OperationChange op = new OperationChange();
        op.setModificationTypeName(OperationChange.Modification.RENAME);
        op.setName("echo"); 
        op.setNewName("echoDelayer");     
        ParameterChange parameter = new ParameterChange();
        parameter.setModificationType(ParameterChange.Modification.RENAME);
        parameter.setName("arg0");
        parameter.setNewName("argRename0");
        op.addParameter(parameter);
        wsdlTransformation.addOperation(op);
       
        String testName = "OperationRenameParameterRename"; 
        String inXSL = "Templates/" + testName + ".xsl";
        String inXML = "SOAPEnvelope/" + testName + ".xml";
        String expectedOutXML = "SOAPEnvelope/" + testName + "Expected.xml";
        String outXML = "SOAPEnvelope/" + testName + "Transformed.xml";
      
        /*** Asserts ***/
        XSLManager xslManager = new XSLManager();
        boolean error = xslManager.createXSL(wsdlTransformation,inXSL);		
        assertTrue(!error);	
        
		error = SOAPTransform.SOAPTransformation(inXML, inXSL, outXML);
		assertTrue(!error);
		
		String stringExpectedOutXML = readFile(expectedOutXML);
		String stringOutXML = readFile(outXML);	
		assertNotNull(stringExpectedOutXML);
		assertNotNull(stringOutXML);
		
		stringExpectedOutXML= stringExpectedOutXML.replaceAll("\\s+", "");	
		stringOutXML= stringOutXML.replaceAll("\\s+", "");	
		
		assertEquals(stringExpectedOutXML, stringOutXML);

	}
	
	
	@Test
	public void operationNoneParameterAdd_Rename() {
		WsdlTransformation wsdlTransformation = new WsdlTransformation();
        OperationChange op = new OperationChange();
        op.setModificationTypeName(OperationChange.Modification.ONLYPARAMETERCHANGES); 
        
        ParameterChange parameterAdd = new ParameterChange();
        parameterAdd.setModificationType(ParameterChange.Modification.NEW);
        parameterAdd.setPreviousNameNode("argCeroViejo");
        List<Parameter> list = new LinkedList<Parameter>();
        Parameter parameter1 = new Parameter("argUnoNuevo", "valueUno");
        Parameter parameter2 = new Parameter("argDosNuevo", "valueDos");
        Parameter parameter3 = new Parameter("argTresNuevo", "valueTres");
        list.add(parameter1);
        list.add(parameter2);
        list.add(parameter3);
        parameterAdd.setParametersToInsert(list);
        
        ParameterChange parameterRename = new ParameterChange();
        parameterRename.setModificationType(ParameterChange.Modification.RENAME);
        parameterRename.setName("argTresViejo");
        parameterRename.setNewName("argCuatroRename");

        op.addParameter(parameterAdd);
        op.addParameter(parameterRename);
        wsdlTransformation.addOperation(op);
       
        String testName = "OperationNoneParameterAdd_Rename"; 
        String inXSL = "Templates/" + testName + ".xsl";
        String inXML = "SOAPEnvelope/" + testName + ".xml";
        String expectedOutXML = "SOAPEnvelope/" + testName + "Expected.xml";
        String outXML = "SOAPEnvelope/" + testName + "Transformed.xml";
      
        /*** Asserts ***/
        XSLManager xslManager = new XSLManager();
        boolean error = xslManager.createXSL(wsdlTransformation,inXSL);		
        assertTrue(!error);	
        
		error = SOAPTransform.SOAPTransformation(inXML, inXSL, outXML);
		assertTrue(!error);
		
		String stringExpectedOutXML = readFile(expectedOutXML);
		String stringOutXML = readFile(outXML);	
		assertNotNull(stringExpectedOutXML);
		assertNotNull(stringOutXML);
		
		stringExpectedOutXML= stringExpectedOutXML.replaceAll("\\s+", "");	
		stringOutXML= stringOutXML.replaceAll("\\s+", "");	
		
		assertEquals(stringExpectedOutXML, stringOutXML);

	}


	@Test
	public void operationRenameParameterDelete() {
		WsdlTransformation wsdlTransformation = new WsdlTransformation();
        OperationChange op = new OperationChange();
        op.setModificationTypeName(OperationChange.Modification.ONLYPARAMETERCHANGES);
  
        ParameterChange parameter = new ParameterChange();
        parameter.setModificationType(ParameterChange.Modification.DELETE);
        parameter.setName("aborrar");
        op.addParameter(parameter);
        wsdlTransformation.addOperation(op);
       
        String testName = "OperationNoneParameterDelete"; 
        String inXSL = "Templates/" + testName + ".xsl";
        String inXML = "SOAPEnvelope/" + testName + ".xml";
        String expectedOutXML = "SOAPEnvelope/" + testName + "Expected.xml";
        String outXML = "SOAPEnvelope/" + testName + "Transformed.xml";
      
        /*** Asserts ***/
        XSLManager xslManager = new XSLManager();
        boolean error = xslManager.createXSL(wsdlTransformation,inXSL);		
        assertTrue(!error);	
        
		error = SOAPTransform.SOAPTransformation(inXML, inXSL, outXML);
		assertTrue(!error);
		
		String stringExpectedOutXML = readFile(expectedOutXML);
		String stringOutXML = readFile(outXML);	
		assertNotNull(stringExpectedOutXML);
		assertNotNull(stringOutXML);
		
		stringExpectedOutXML= stringExpectedOutXML.replaceAll("\\s+", "");	
		stringOutXML= stringOutXML.replaceAll("\\s+", "");	
		
		assertEquals(stringExpectedOutXML, stringOutXML);

	}
	
	@Test
	public void operationDelete() {
		WsdlTransformation wsdlTransformation = new WsdlTransformation();
        OperationChange op = new OperationChange();
        op.setModificationTypeName(OperationChange.Modification.DELETE);
        op.setName("tes:echo2");
        wsdlTransformation.addOperation(op);
          
        String testName = "OperationDelete"; 
        String inXSL = "Templates/" + testName + ".xsl";
        String inXML = "SOAPEnvelope/" + testName + ".xml";
        String expectedOutXML = "SOAPEnvelope/" + testName + "Expected.xml";
        String outXML = "SOAPEnvelope/" + testName + "Transformed.xml";
      
        /*** Asserts ***/
        XSLManager xslManager = new XSLManager();
        boolean error = xslManager.createXSL(wsdlTransformation,inXSL);		
        assertTrue(!error);	
        
		error = SOAPTransform.SOAPTransformation(inXML, inXSL, outXML);
		assertTrue(!error);
		
		String stringExpectedOutXML = readFile(expectedOutXML);
		String stringOutXML = readFile(outXML);	
		assertNotNull(stringExpectedOutXML);
		assertNotNull(stringOutXML);
		
		stringExpectedOutXML= stringExpectedOutXML.replaceAll("\\s+", "");	
		stringOutXML= stringOutXML.replaceAll("\\s+", "");	
		
		assertEquals(stringExpectedOutXML, stringOutXML);

	}
	
	@Test
	public void operationNoneParameterAddFirst() {
		WsdlTransformation wsdlTransformation = new WsdlTransformation();
        
		OperationChange op1 = new OperationChange();
        op1.setModificationTypeName(OperationChange.Modification.ONLYPARAMETERCHANGES); 
        op1.setName("tes:echo1");
        ParameterChange parameterAdd1 = new ParameterChange();
        parameterAdd1.setModificationType(ParameterChange.Modification.NEW);
        parameterAdd1.setPreviousNameNode(null);
        List<Parameter> list1 = new LinkedList<Parameter>();
        Parameter parameter1 = new Parameter("first1", "value1");   
        list1.add(parameter1);
        parameterAdd1.setParametersToInsert(list1);
        op1.addParameter(parameterAdd1);
        wsdlTransformation.addOperation(op1);
        
        OperationChange op2 = new OperationChange();
        op2.setModificationTypeName(OperationChange.Modification.ONLYPARAMETERCHANGES); 
        op2.setName("tes:echo2");
        ParameterChange parameterAdd2 = new ParameterChange();
        parameterAdd2.setModificationType(ParameterChange.Modification.NEW);
        parameterAdd2.setPreviousNameNode(null);
        List<Parameter> list2 = new LinkedList<Parameter>();
        Parameter parameter2 = new Parameter("first2", "value2");   
        Parameter parameter22 = new Parameter("first22", "value22");   
        list2.add(parameter2);
        list2.add(parameter22);
        parameterAdd2.setParametersToInsert(list2);
        op2.addParameter(parameterAdd2);
        wsdlTransformation.addOperation(op2);
       
        OperationChange op3 = new OperationChange();
        op3.setModificationTypeName(OperationChange.Modification.ONLYPARAMETERCHANGES); 
        op3.setName("tes:echo3");
        ParameterChange parameterAdd3 = new ParameterChange();
        parameterAdd3.setModificationType(ParameterChange.Modification.NEW);
        parameterAdd3.setPreviousNameNode(null);
        List<Parameter> list3 = new LinkedList<Parameter>();
        Parameter parameter3 = new Parameter("first3", "value3");     
        list3.add(parameter3);
        parameterAdd3.setParametersToInsert(list3);
        op3.addParameter(parameterAdd3);
        wsdlTransformation.addOperation(op3);
       
        String testName = "OperationNoneParameterAddFirst"; 
        String inXSL = "Templates/" + testName + ".xsl";
        String inXML = "SOAPEnvelope/" + testName + ".xml";
        String expectedOutXML = "SOAPEnvelope/" + testName + "Expected.xml";
        String outXML = "SOAPEnvelope/" + testName + "Transformed.xml";
      
        /*** Asserts ***/
        XSLManager xslManager = new XSLManager();
        boolean error = xslManager.createXSL(wsdlTransformation,inXSL);		
        assertTrue(!error);	
        
		error = SOAPTransform.SOAPTransformation(inXML, inXSL, outXML);
		assertTrue(!error);
		
		String stringExpectedOutXML = readFile(expectedOutXML);
		String stringOutXML = readFile(outXML);	
		assertNotNull(stringExpectedOutXML);
		assertNotNull(stringOutXML);
		
		stringExpectedOutXML= stringExpectedOutXML.replaceAll("\\s+", "");	
		stringOutXML= stringOutXML.replaceAll("\\s+", "");	
		
		assertEquals(stringExpectedOutXML, stringOutXML);

	}

	 private  String readFile(String file){
		 try{
		    BufferedReader reader = new BufferedReader( new FileReader (file));
		    String line  = null;
		    StringBuilder stringBuilder = new StringBuilder();
		    String ls = System.getProperty("line.separator");
		    while( ( line = reader.readLine() ) != null ) {
		        stringBuilder.append( line );
		        stringBuilder.append( ls );
		    }
		    return stringBuilder.toString();
		 }catch (Exception ex){
			 ex.printStackTrace();
			 return null;
		 }
	 }

}
